global wavelist 2015H2 2016H1 2016H2 2017H1 2017H2



*--- 0. Open data
use "${datadir}\HTB_main", clear	


*--- 1. Merge with stock 
rename yearbirth dob


// 2015H1
merge m:1 pcd dob using "${datadir_psd}\work_psd_stock_2015H1", keep(1 3) ///
	gen(merge_psd007_2015H1) keepusing(frnoflendcurr isthecurrrate irate_type)

rename frnoflendcurr frnolend_2015H1
rename isthecurrrate isthecurrrate_2015H1
rename irate_type irate_type_2015H1

gen incent_2015H1 = isthecurrrate_2015H1 == "Y"

gen incenti_2015H1 = incent_2015H1 & lender_id == frnolend_2015H1
gen incente_2015H1 = incent_2015H1 & lender_id != frnolend_2015H1


// other waves
foreach x in $wavelist {
	merge m:1 pcd dob using "${datadir_psd}\work_psd_stock_`x'", keep(1 3) ///
		gen(merge_psd007_`x') keepusing(frnoflendcurr isthecurrrati irate_type)

	rename frnoflendcurr frnolend_`x'
	rename isthecurrrati isthecurrrate_`x'
	rename irate_type irate_type_`x'

	gen incent_`x' = isthecurrrate_`x' == "Current Rate Incentivised - Yes"
	
	gen incenti_`x' = incent_`x' & lender_id == frnolend_`x'
	gen incente_`x' = incent_`x' & lender_id != frnolend_`x'
}



*--- 2. Define target (3 measures)

gen mygroup0 = ///
	cond(inrange(dateends, mdy(1, 1, 2015), mdy(6, 30, 2015)), "2015H1", ///
	cond(inrange(dateends, mdy(7, 1, 2015), mdy(12, 31, 2015)), "2015H2", ///
	cond(inrange(dateends, mdy(1, 1, 2016), mdy(6, 30, 2016)), "2016H1", ///
	cond(inrange(dateends, mdy(7, 1, 2016), mdy(12, 31, 2016)), "2016H2", ///
	cond(inrange(dateends, mdy(1, 1, 2017), mdy(6, 30, 2017)), "2017H1", ///
	cond(inrange(dateends, mdy(7, 1, 2017), mdy(12, 31, 2017)), "2017H2", ""))))))
	
gen mygroup3 = ///
	cond(inrange(dateends, mdy(10, 1, 2014), mdy(3, 31, 2015)), "2015H1", ///
	cond(inrange(dateends, mdy(4, 1, 2015), mdy(9, 30, 2015)), "2015H2", ///
	cond(inrange(dateends, mdy(10, 1, 2015), mdy(3, 31, 2016)), "2016H1", ///
	cond(inrange(dateends, mdy(4, 1, 2016), mdy(9, 30, 2016)), "2016H2", ///
	cond(inrange(dateends, mdy(10, 1, 2016), mdy(3, 31, 2017)), "2017H1", ///
	cond(inrange(dateends, mdy(4, 1, 2017), mdy(9, 30, 2017)), "2017H2", ""))))))
	
gen mygroup6 = ///
	cond(inrange(dateends, mdy(7, 1, 2014), mdy(12, 31, 2014)), "2015H1", ///
	cond(inrange(dateends, mdy(1, 1, 2015), mdy(6, 30, 2015)), "2015H2", ///
	cond(inrange(dateends, mdy(7, 1, 2015), mdy(12, 31, 2015)), "2016H1", ///
	cond(inrange(dateends, mdy(1, 1, 2016), mdy(6, 30, 2016)), "2016H2", ///
	cond(inrange(dateends, mdy(7, 1, 2016), mdy(12, 31, 2016)), "2017H1", ///
	cond(inrange(dateends, mdy(1, 1, 2017), mdy(6, 30, 2017)), "2017H2", ""))))))


forvalues i = 0(3)6 {
	gen incent`i' = 0
	gen incenti`i' = 0
	gen incente`i' = 0
	gen merge_psd007_`i' = 0
	foreach x in $wavelist {
		replace incent`i' = 1 if mygroup`i' == "`x'" & incent_`x' == 1
		replace incenti`i' = 1 if mygroup`i' == "`x'" & incenti_`x' == 1
		replace incente`i' = 1 if mygroup`i' == "`x'" & incente_`x' == 1
		
		replace merge_psd007_`i' = merge_psd007_`x' if mygroup`i' == "`x'"
		gen htb_dummy_`x'_`i' = htb_dummy & mygroup`i' == "`x'"
	}	
} 



		
*--- 3. Regression with one htb_dummy category
gen constant = 1
encode laua, gen(la)
gen gross_income_log = log(gross_income)


	
forvalues i = 0(3)6 {
	// All refinancing
	eststo clear

	
	reghdfe incent`i' htb_dummy i.lender_id ///
		gross_income_log age_borrower i.advtype ///
		i.employment i.income_basis purchaseprice ///
		if merge_psd007_`i' == 3 & subsample, ///
		absorb(la datem) vce(cluster la datem) // to get e(sample)
	gen mysample`i' = e(sample)

	eststo: reghdfe incent`i' htb_dummy ///
		if mysample`i', ///
		absorb(constant) vce(cluster la datem)
		estadd local controls 	""
		estadd local FE 	""
		estadd local cluster "LA, sale month"
		
	eststo: reghdfe incent`i' htb_dummy i.lender_id ///
		gross_income_log age_borrower i.advtype ///
		i.employment i.income_basis purchaseprice ///
		if mysample`i', ///
		absorb(la datem) vce(cluster la datem)
		estadd local controls 	"Yes"
		estadd local FE 	"LA, sale month"
		estadd local cluster "LA, sale month"
		
	// Same-lender refinancing
	eststo: reghdfe incenti`i' htb_dummy ///
		if mysample`i', ///
		absorb(constant) vce(cluster la datem)
		estadd local controls 	""
		estadd local FE 	""
		estadd local cluster "LA, sale month"
		
	eststo: reghdfe incenti`i' htb_dummy i.lender_id ///
		gross_income_log age_borrower i.advtype ///
		i.employment i.income_basis purchaseprice ///
		if mysample`i', ///
		absorb(la datem) vce(cluster la datem)
		estadd local controls 	"Yes"
		estadd local FE 	"LA, sale month"
		estadd local cluster "LA, sale month"
		
	// Different-lender refinancing
	eststo: reghdfe incente`i' htb_dummy ///
		if mysample`i', ///
		absorb(constant) vce(cluster la datem)
		estadd local controls 	""
		estadd local FE 	""
		estadd local cluster "LA, sale month"

	eststo: reghdfe incente`i' htb_dummy i.lender_id ///
		gross_income_log age_borrower i.advtype ///
		i.employment i.income_basis purchaseprice ///
		if mysample`i', ///
		absorb(la datem) vce(cluster la datem)
		estadd local controls 	"Yes"
		estadd local FE 	"LA, sale month"
		estadd local cluster "LA, sale month"
			
		
	// Regression output
	esttab using "${tabledir}\rmg_r6_new_`i'.tex", b(3) se star(* 0.10 ** 0.05 *** 0.01) ///
		keep(htb_dummy) replace ///
		stats(controls FE cluster meany r2 N, fmt (0 0 0  %4.2fc %4.2fc %10.0fc) ///
		labels( `"\textsc{Regression controls}"' `"\textsc{Fixed effects}"' ///
		`"\textsc{SE clustering}"' `"\textsc{Mean Y}"') )  ///
		mtitle("All" "All" "Same lender" "Same lender" "Different lender" "Different lender")  	
} 